package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"zk/zg3_zk2/src/pro/global"
	"zk/zg3_zk2/src/pro/internal/models"
	"zk/zg3_zk2/src/pro/internal/params"
)

func SearchVideos(c *gin.Context) {
	var err error
	var req params.SearchVideosReq
	var v models.Videos

	if err = c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	find, _ := v.Search(req.Page, req.PageSize, req.Keyword)

	if len(find) == 0 {
		c.JSON(http.StatusNotFound, gin.H{"error": "未找到相关视频！"})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "搜索成功",
		"data":    find,
	})
}

func VideInfo(c *gin.Context) {
	var err error
	var req params.VideosInfoReq
	var v models.Videos

	if err = c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	if err = global.DB.Where("id=?", req.Id).Find(&v).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":  404,
			"error": "该用户不存在",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "视频详情成功",
		"data":    v,
	})
}
